package com.mcptt.db.model;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.db.DbConst;

import java.io.Serializable;

public class MsgStatusInfo implements Serializable
{
	public static final String Sys_Num = "0000000000";
	public static final String KEY = "addrInfo";
	public static final int GroupType_Default = 1;
	public static final int GroupType_Normal = 2;
	public static final int GroupType_Incomplete = 9;

	public static final int UserType_Sys = 10;
	public static final int UserType_Dac = 11;
	public static final int UserType_User = 12;
	public static final int UserType_Camera = 13;

	public static final int UserType_Ai_Car = 15;
	public static final int UserType_Ai_Plane = 16;
	public static final int UserType_Default = 19;

	public static final int DEFAULT_ATTR = 0;

	private static String SvrAddrDesc = "crsc.mcx.mnc020.mcc460.5gr";//BootStrapSvrInfo.Default_Svr_Ip +":"+ BootStrapSvrInfo.Default_Svr_Port;
	protected int type;
	protected String addrId;
	protected String name;
//	protected String opNum;
	protected int attr = 0;


	public static void setSvrAddrDesc(String svrIp,int port) {
		SvrAddrDesc = svrIp + ":" + port;
	}

	public MsgStatusInfo()
	{

	}
	public MsgStatusInfo(int type, String name, String mcId, int attr)
	{
		this.type = type;
		this.addrId = mcId;
		if(name == null
				|| name.trim().length() <= 0)
		{
			this.name = mcId;
		}
		else
		{
			this.name = name;
		}
		this.attr = attr;
	}
	public MsgStatusInfo(Cursor c)
	{
		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args);
	}

	
	public boolean isGroupType()
	{
		if(type == MsgStatusInfo.GroupType_Default
				|| type == MsgStatusInfo.GroupType_Normal
				|| type == MsgStatusInfo.GroupType_Incomplete)
		{
			return true;
		}
		return false;
	}

	public boolean isUserType()
	{
		if(type == MsgStatusInfo.UserType_Sys
				|| type == MsgStatusInfo.UserType_Dac
				|| type == MsgStatusInfo.UserType_User)
		{
			return true;
		}
		return false;
	}
	public int getType() {
		return type;
	}
	
	public void setType(int userType) {
		this.type = userType;
	}

	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}



	public void setAddrId(String addrId) {
		this.addrId = addrId;
	}

	public String getAddrId() {
		return addrId;
	}

	public String getMcxId() {
		return MsgStatusInfo.toSipUrlFromNum(addrId);
	}

//	protected void updateOpNum(String num) {
//		this.opNum = num;
//	}
//	public String getOpNum() {
//		return opNum;
//	}
	
	public String getDscp()
	{
		if(name != null
				&& name.length() > 0)
		{
			return name;
		}
		if(addrId != null
				&& addrId.length() > 0)
		{
			return addrId;
		}
		else
		{
			return "";
		}
	}
	public static String getDefaultName(String mcId)
	{
		return "User_" + mcId;
	}
	public int getAttr() {
		return attr;
	}

	public void setAttr(int attr) {
		this.attr = attr;
	}

	public boolean isSys() {
		if(type == UserType_Sys)
		{
			return true;
		}
		return false;
	}
	public boolean isDac() {
		if(type == UserType_Dac)
		{
			return true;
		}
		return false;
	}
	public boolean isCamera() {
		if(type == UserType_Camera)
		{
			return true;
		}
		return false;
	}
	public boolean isAi() {
		if(type == UserType_Ai_Car
				|| type == UserType_Ai_Plane)
		{
			return true;
		}
		return false;
	}
	public boolean isSameOne(MsgStatusInfo oldInfo)
	{
		if(oldInfo == null)
		{
			return false;
		}
		String oldMcId = oldInfo.addrId;
		if(this.addrId != null
				&& this.addrId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(UserInfo oldInfo)
	{
		if(oldInfo == null)
		{
			return false;
		}
		String oldMcId = oldInfo.getAddrId();
		if(this.addrId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(McIdInfo mcIdInfo)
	{
		if(mcIdInfo == null)
		{
			return false;
		}
		String oldMcId = mcIdInfo.getAddrId();
		if(this.addrId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(String oldMcId)
	{
		if(this.addrId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public static String[] getColumns() {
		return new String[]{DbConst.COL_AddrId,DbConst.COL_Type,DbConst.COL_Name,DbConst.COL_Attr};
	}

	public void createFromContentValue(ContentValues args) {

//		Log.debug(this.getClass(), "createFromContentValue");
		String tmp_s;

		tmp_s = args.getAsString(DbConst.COL_AddrId);
		if(tmp_s != null) {
			addrId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Type);
		if(tmp_s != null) {
			type = CheckData.parseInt(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Name);
		if(tmp_s != null) {
			name = tmp_s;
		}
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("userType = ").append(type).append(",");
		strBuf.append("mcId = ").append(addrId).append(",");
		strBuf.append("name = ").append(name).append(".");


		return strBuf.toString();
	}


	protected static final String BeginKey = "sip:";
	protected static final String EndKey = "@";

	public static String toImsImpi(String num,String domain)
	{
//		Log.i(THIS_FILE, "toSipNum num = " +num + " endStr = " + endStr);
		StringBuffer strBuf = new StringBuffer();
		strBuf.append(num);
		strBuf.append(EndKey);
		strBuf.append(domain);

		Log.debug(MsgStatusInfo.class, "toSipNum = " +strBuf.toString() );

		return strBuf.toString();
	}

	public static String toImsImpu(String num,String domain)
	{
		Log.debug(MsgStatusInfo.class, "toImsImpu num = " +num);
		StringBuffer strBuf = new StringBuffer(BeginKey);
		strBuf.append(num);
		strBuf.append(EndKey);
		strBuf.append(domain);

		Log.debug(MsgStatusInfo.class, "toImsImpu = " +strBuf.toString() );
		return strBuf.toString();
	}

	public static String toMcxPsi(String keyStr,String domain)
	{
		Log.debug(MsgStatusInfo.class, "toMcxPsi keyStr = " + keyStr + " domain = " + domain);
		if(keyStr == null
				|| keyStr.length() <= 0)
		{
			return "";
		}
		if(domain == null
				|| domain.length() <= 0)
		{
			return "";
		}
		StringBuffer strBuf = new StringBuffer(BeginKey);
		strBuf.append(keyStr).append(EndKey).append(domain);
		return strBuf.toString();//去掉前面�?0
	}

	public static String toNumFromSipUrl(String urlStr)
	{
		Log.debug(MsgStatusInfo.class, "convertSipUrlToNum urlStr = " + urlStr);
		if(urlStr == null
				|| urlStr.length() <= 0)
		{
			return "";
		}
		int beginIndex = urlStr.indexOf(BeginKey);
		int endIndex = urlStr.indexOf(EndKey);
		if(beginIndex == -1
				|| endIndex == -1
				|| beginIndex > endIndex)
		{
			return "";
		}
		beginIndex = beginIndex + BeginKey.length();
		urlStr = urlStr.substring(beginIndex, endIndex);
		Log.debug(MsgStatusInfo.class, "convertSipUrlToNum end urlStr = " + urlStr);
		return urlStr;//去掉前面�?0
	}


	
	public static String toSipUrlFromNum(String num)
	{
		StringBuffer strBuf = new StringBuffer("<");
		strBuf.append(BeginKey).append(num).append(EndKey).append(SvrAddrDesc).append(">");
		return strBuf.toString();
	}

	public static String toUrlFromNum(String num)
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append(BeginKey).append(num).append(EndKey).append(SvrAddrDesc);
		return strBuf.toString();
	}

}
